package com.bj.dz.controller;

import com.bj.dz.ResultModel.R;
import com.bj.dz.ResultModel.RHelper;
import com.bj.dz.ResultModel.ResponseCode;
import com.bj.dz.entity.ReleaseSubtask;
import com.bj.dz.entity.ReleaseTask;
import com.bj.dz.service.ReleaseSubtaskService;
import com.bj.dz.service.ReleaseTaskService;
import com.bj.dz.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 *   子任务控制器
 */
@Slf4j
@Api(tags = "【子任务】提供服务接口")
@RestController
@RequestMapping("/releaseSubtask")
public class ReleaseSubtaskController {

    @Resource
    private ReleaseSubtaskService releaseSubtaskService;

    @Resource
    private ReleaseTaskService releaseTaskService;

    /**
     * 查询所有发布任务【根据模块(1建筑物2水3汽4交通5电力6通讯)以及状态为1(发布的)-----7(入库状态)】【使用2】
     * 通过模块type和状态state【使用2】
     * 没有传入状态查询所有
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查询任务,根据不同模块和状态")
    @RequestMapping(value = "/findAllByType", method = RequestMethod.GET, produces = "application/json;" + "charset=UTF-8")
    public Page findAllByType(@ApiParam(name = "type", value = "模块类型【1建筑物2水3汽4交通5电力6通讯】", required = true)
                              @RequestParam(value = "type", required = true) Integer type,
                              @ApiParam(name = "state", value = "任务状态【1发布2认领3数据上传4提交质检5质检不合格6质检合格7数据入库】", required = false) @RequestParam(value = "state", required = false) Integer state,
                              @ApiParam(name = "page", value = "当前页", required = true) @RequestParam(value = "page", required = true) Integer page,
                              @ApiParam(name = "rows", value = "每页显示行数", required = true) @RequestParam(value = "rows", required = true) Integer rows) {
        log.info("模块类型type为:" + type);
        //查询所有发布任务根据模块类型
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("type", type);//模块类型
        if (state != null && state != 0) {
            param.put("State", state);
        }//设置状态
        //param.put("State", Constant.RELEASE_TASK_STATE_1);//状态为1
        Page<ReleaseSubtask> pageData = releaseSubtaskService.queryReleaseSubtaskPageByMap(param, page, rows);//分页相关的一切数据
        return pageData;
    }

    /**
     * 通过id查询
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查找服务")
    @RequestMapping(value = "/findById", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public ReleaseSubtask findReleaseSubtaskById(@ApiParam(name = "id", value = "ReleaseSubtask的id", required = true) @RequestParam(value = "id", required = true) Integer id) {
        return releaseSubtaskService.getReleaseSubtaskById(id);
    }

    /**
     * 通过多个查询
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查找多个服务")
    @RequestMapping(value = "/findReleaseSubtasks", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public List<ReleaseSubtask> findReleaseSubtaskListByMap(@ApiParam(name = "ReleaseSubtask", value = "releaseSubtask", required = true) @RequestBody ReleaseSubtask releaseSubtask) {

        return releaseSubtaskService.getReleaseSubtaskListByMap(null);
    }

    /**
     * 查询总数
     *
     * @param
     * @return
     */
    @ApiOperation(value = "获取总数服务")
    @RequestMapping(value = "/count", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public Integer ReleaseSubtaskCount(@ApiParam(name = "type", value = "模块类型【1建筑物2水3汽4交通5电力6通讯】", required = true) @RequestParam(value = "type", required = true) Integer type) {
        log.info("模块类型type为:" + type);
        //查询所有发布任务根据模块类型
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("type", type);//模块类型
        return releaseSubtaskService.getReleaseSubtaskCountByMap(param);
    }

    /**
     * 添加保存
     *
     * @param
     * @return
     */
    @ApiOperation(value = "添加服务")
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public boolean addReleaseSubtask(@ApiParam(name = "ReleaseSubtask", value = "releaseSubtask对象", required = true) @RequestBody ReleaseSubtask releaseSubtask) {
        System.out.println(releaseSubtask);
        return releaseSubtaskService.addReleaseSubtask(releaseSubtask);
    }

    /**
     * 更新修改 更新子任务(用户id，任务id，子任务id，电话，姓名
     * 主要更新电话和姓名
     *
     * @param
     * @return
     */
    @ApiOperation(value = "更新子任务(用户id，任务id，子任务id，电话，姓名【更新电话和姓名以及备注[备注可以为null]】)【使用】")
    @RequestMapping(value = "/upd1", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public R modifyReleaseSubtask1(@ApiParam(name = "userId", value = "用户的id", required = true) @RequestParam(value = "userId", required = true) Integer userId,
                                   @ApiParam(name = "type", value = "任务id", required = true) @RequestParam(value = "type", required = true) Integer type,
                                   @ApiParam(name = "id", value = "子任务id", required = true) @RequestParam(value = "id", required = true) Integer id,
                                   @ApiParam(name = "phone", value = "电话", required = true) @RequestParam(value = "phone", required = true) String phone,
                                   @ApiParam(name = "name", value = "姓名", required = true) @RequestParam(value = "name", required = true) String name,
                                   @ApiParam(name = "remark", value = "备注") @RequestParam(value = "remark", required = false) String remark) {
        R r = RHelper.ok();
        //查询用户用没有任务
        Map<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        List<ReleaseTask> tlist = releaseTaskService.getReleaseTaskListByMap(param);
        if (tlist == null || tlist.isEmpty()) {
            r.setMsg("更新失败,未找到对应的任务,检查传入的用户id");
            r.setCode(ResponseCode.FAILED_CODE);
            return r;
        } else {
            boolean flag0 = false;
            //查看任务是不是属于这个用户的
            for (ReleaseTask releaseTask : tlist) {
                //获取任务id
                int taskId = releaseTask.getId();
                if (taskId == type) {
                    flag0 = true;
                }
            }
            if (!flag0) {
                r.setMsg("更新失败,任务不属于该用户");
                r.setCode(ResponseCode.FAILED_CODE);
                return r;
            }
        }

        //查询任务下有没有对应的子任务id
        Map<String, Object> param2 = new HashMap<>();
        param2.put("type", type);
        List<ReleaseSubtask> stList = releaseSubtaskService.getReleaseSubtaskListByMap(param2);
        if (stList == null || stList.isEmpty()) {
            r.setMsg("更新失败,未找任务id到对应的子任务");
            r.setCode(ResponseCode.FAILED_CODE);
            return r;
        } else {
            boolean flag0 = false;
            //查看任务是不是属于这个用户的
            for (ReleaseSubtask buildingReleaseSubtask : stList) {
                //获取子任务id
                int zid = buildingReleaseSubtask.getId();
                if (zid == id) {
                    flag0 = true;
                }
            }
            if (!flag0) {
                r.setMsg("更新失败,子任务不属于该任务id下，请检查任务id是否正确");
                r.setCode(ResponseCode.FAILED_CODE);
                return r;
            }
        }

        //先查对应的子任务
        ReleaseSubtask releaseSubtask = releaseSubtaskService.getReleaseSubtaskById(id);
        if (releaseSubtask == null) {
            r.setMsg("更新失败,未找到对应的子任务");
            r.setCode(ResponseCode.FAILED_CODE);
            return r;
        } else {
            releaseSubtask.setPhone(phone);
            releaseSubtask.setName(name);
            if (remark != null && !remark.equals("")) {
                releaseSubtask.setRemark(remark);
            }
            boolean flag = releaseSubtaskService.modifyReleaseSubtask(releaseSubtask);
            if (flag) {
                r.setMsg("更新成功");
            } else {
                r.setMsg("更新失败");
                r.setCode(ResponseCode.FAILED_CODE);
                return r;
            }
        }
        return r;
    }

    /**
     * 更新修改 更新子任务表接口 （用户id，任务id，子任务id，范围）【更新范围】
     *
     * @param
     * @return
     */
    @ApiOperation(value = "更新子任务(用户id，任务id，子任务id，范围【更新范围】)【使用】")
    @RequestMapping(value = "/upd2", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public R modifyReleaseSubtask2(@ApiParam(name = "userId", value = "用户的id", required = true) @RequestParam(value = "userId", required = true) Integer userId,
                                   @ApiParam(name = "type", value = "任务id", required = true) @RequestParam(value = "type", required = true) Integer type,
                                   @ApiParam(name = "id", value = "子任务id", required = true) @RequestParam(value = "id", required = true) Integer id,
                                   @ApiParam(name = "ranges", value = "范围", required = true) @RequestParam(value = "ranges", required = true) String ranges) {
        R r = RHelper.ok();
        //查询用户用没有任务
        Map<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        List<ReleaseTask> tlist = releaseTaskService.getReleaseTaskListByMap(param);
        if (tlist == null || tlist.isEmpty()) {
            r.setMsg("更新失败,未找到对应的任务,检查传入的用户id");
            r.setCode(ResponseCode.FAILED_CODE);
            return r;
        } else {
            boolean flag0 = false;
            //查看任务是不是属于这个用户的
            for (ReleaseTask releaseTask : tlist) {
                //获取任务id
                int taskId = releaseTask.getId();
                if (taskId == type) {
                    flag0 = true;
                }
            }
            if (!flag0) {
                r.setMsg("更新失败,任务不属于该用户");
                r.setCode(ResponseCode.FAILED_CODE);
                return r;
            }
        }
        //查询任务下有没有对应的子任务id
        Map<String, Object> param2 = new HashMap<>();
        param2.put("type", type);
        List<ReleaseSubtask> stList = releaseSubtaskService.getReleaseSubtaskListByMap(param2);
        if (stList == null || stList.isEmpty()) {
            r.setMsg("更新失败,未找任务id到对应的子任务");
            r.setCode(ResponseCode.FAILED_CODE);
            return r;
        } else {
            boolean flag0 = false;
            //查看任务是不是属于这个用户的
            for (ReleaseSubtask releaseSubtask : stList) {
                //获取子任务id
                int zid = releaseSubtask.getId();
                if (zid == id) {
                    flag0 = true;
                }
            }
            if (!flag0) {
                r.setMsg("更新失败,子任务不属于该任务id下，请检查任务id是否正确");
                r.setCode(ResponseCode.FAILED_CODE);
                return r;
            }
        }

        //先查对应的子任务
        ReleaseSubtask releaseSubtask = releaseSubtaskService.getReleaseSubtaskById(id);
        if (releaseSubtask == null) {
            r.setMsg("更新失败,未找到对应的子任务");
            r.setCode(ResponseCode.FAILED_CODE);
            return r;
        } else {
            releaseSubtask.setRanges(ranges);
            boolean flag = releaseSubtaskService.modifyReleaseSubtask(releaseSubtask);
            if (flag) {
                r.setMsg("更新成功");
            } else {
                r.setMsg("更新失败");
                r.setCode(ResponseCode.FAILED_CODE);
                return r;
            }
        }
        return r;
    }

    /**
     * 更新修改
     *
     * @param
     * @return
     */
    @ApiOperation(value = "更新修改服务")
    @RequestMapping(value = "/upd", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public boolean modifyReleaseSubtask(@ApiParam(name = "ReleaseSubtask", value = "releaseSubtask对象", required = true) @RequestBody ReleaseSubtask releaseSubtask) {
        return releaseSubtaskService.modifyReleaseSubtask(releaseSubtask);
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @ApiOperation(value = "删除服务")
    @RequestMapping(value = "/del", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public boolean deleteReleaseSubtaskById(@ApiParam(name = "ReleaseSubtask", value = "releaseSubtask的id", required = true) @RequestParam(value = "id", required = true) Integer id) {
        return releaseSubtaskService.deleteReleaseSubtaskById(id);
    }


}
